# get all the DNS record

require "00_common"

module HttpExtractor
  def self.get_all_files
    Common.get_all_files("./Jan_6")
  end

  def self.handle_each_file(file_name)
    puts "now handling : " + file_name
    file = File.open "./Jan_6/" + file_name, "r"
    output_file_name = String.new "dns_raw_" + file_name
    output_file = File.open output_file_name, "w"

    begin
      counter = 0
      while line = file.readline
        counter = counter + 1

        line = line.rstrip
        if line.end_with? "|"
          line = line + "NULL"
        end

        fields = line.split "|"

        local_time_stamp    = Common.check_string fields[LOCAL_TIME_STAMP]
        session_id          = Common.check_string fields[SESSION_ID]
        session_start_time  = Common.check_string fields[SESSION_START_TIME]
        client_ip           = Common.check_string fields[CLIENT_IP]
        server_ip           = Common.check_string fields[SERVER_IP]
        l4_proto            = Common.check_string fields[L4_PROTO]
        client_port         = Common.check_string fields[CLIENT_PORT]
        server_port         = Common.check_string fields[SERVER_PORT]
        # client_payload_hex  = 9
        # server_payload_hex  = 10
        protocol            = Common.check_string fields[PROTOCOL]
        client_payload      = Common.check_string fields[CLIENT_PAYLOAD]
        server_payload      = Common.check_string fields[SERVER_PAYLOAD]

        if protocol.downcase == "dns"
          new_line = "#{local_time_stamp}|#{session_id}|#{session_start_time}|#{client_ip}|#{server_ip}"
          new_line = new_line + "|#{l4_proto}|#{client_port}|#{server_port}|#{protocol}|#{client_payload}|#{server_payload}"

          output_file.puts new_line
        end # of if
        if counter % 100000 == 0
          puts counter
        end
      end
    rescue Exception => e
      puts e.to_s
      file.close
    end

    output_file.close
  end

  def self.handle_all_files(all_files)
    all_files.each do |file_name|
      if Common.file_exist?("./dns_raw",file_name)
        puts "exist file:" + file_name
      else
        handle_each_file(file_name)
        Common.move_file "dns_raw_" + file_name, "./dns_raw"
      end
    end
  end 
end


begin
  all_files = HttpExtractor.get_all_files
  puts all_files.join "\n"

  HttpExtractor.handle_all_files all_files

rescue Exception => e
  puts e.to_s
end